package com.woniuxy.shiro_ini;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.env.BasicIniEnvironment;
import org.apache.shiro.env.Environment;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

/**
 * @Author 罗蔚
 * @Date 2022/4/13 20:01
 * @Description TODO
 */
public class TestIniRealmAuth {
    public static void main(String[] args) {
        //1.读取配置文件,生成一个shiro的环境
        Environment environment = new BasicIniEnvironment("classpath:shiroConfig/shiro-realm.ini");
        //2.通过这个环境，生成默认的SecurityManager对象
        SecurityManager securityManager = environment.getSecurityManager();
        //3.shiro的工具类需要一个SecurityManager的环境
        SecurityUtils.setSecurityManager(securityManager);
        //4.有环境的工具包，将能获取到一个subject对象，然后我们就能使用subject对象来进行业务开发！
        Subject subject = SecurityUtils.getSubject();
        //5.有了主体用户以后，可以直接执行login来进行登录
        subject.login(new UsernamePasswordToken("luowei","123456"));
        //进行登录后的判断
        if (subject.isAuthenticated()){
            System.out.println("恭喜"+subject.getPrincipal()+"用户登录成功!");
        }
        System.out.println("是否有boss的角色："+subject.hasRole("boss"));
        System.out.println("是否有eat::person的权限："+subject.isPermitted("eat::person"));
        System.out.println("是否有user:find的权限："+subject.isPermitted("user:find"));

        //注销
        subject.logout();
        System.out.println("此时用户的认证为:"+subject.isAuthenticated());
        System.out.println("是否有boss的角色："+subject.hasRole("boss"));
        System.out.println("是否有eat::person的权限："+subject.isPermitted("eat::person"));
    }
}
